<html dir=rtl style="-webkit-writing-mode:vertical-lr">
    <head>
        <script src="../../resources/js-test.js"></script>
        <script src="../../resources/gesture-util.js"></script>
        <script language="javascript">
            window.jsTestIsAsync = true;

            function log(str)
            {
                var li = document.createElement("li");
                li.appendChild(document.createTextNode(str));
                var console = document.getElementById("console");
                console.appendChild(li);
            }

            function assertEqual(message, actual, expected)
            {
                if (actual != expected)
                    log(message + ": Failure, actual: " + actual + "; expected: " + expected);
                else
                    log(message + ": Success");
            }

            function assertClose(message, actual, expected)
            {
                if ((actual >= expected && actual - expected < 5) ||
                    (expected >= actual && expected - actual < 5))
                    log(message + ": Success");
                else
                    log(message + ": Failure, actual: " + actual + "; expected: " + expected);
            }

            var expectedScrollX = 500;
            var expectedScrollY = -1000;

            onload = async function()
            {
                if (window.testRunner)
                    testRunner.dumpAsText();

                // Test vertical scrollbar presence and that it is initially at the bottom for RTL page.
                assertEqual("original scroll y for RTL page ", window.scrollY, 0);

                // Test horizontal scroll.
                window.scrollTo(expectedScrollX, expectedScrollY);
                assertEqual("vertical scroll: ", window.scrollY, expectedScrollY);
                // Test calling window.scrollY continuously wont change the scrollY position.
                assertEqual("continuously call window.scrollY ", window.scrollY, expectedScrollY);


                // Test zooming wont reset scroll position.
                if (window.testRunner) {
                    window.scrollTo(expectedScrollX, expectedScrollY);
                    testRunner.zoomPageIn();
                    testRunner.zoomPageOut();
                    assertClose("zoom in and out preserve scroll position", window.scrollY, expectedScrollY);
                }

                // Test resize wont reset scroll position.
                const oldHeight = window.innerHeight;
                window.resizeTo(window.innerWidth, oldHeight - 100);
                window.scrollTo(expectedScrollX, expectedScrollY);
                window.resizeTo(window.innerWidth, oldHeight);
                assertEqual("resize preserves scroll position", window.scrollY, expectedScrollY);

                // Test HOME/END operation.
                if (window.eventSender) {
                    // Note: in Mac, HOME/END key resets the scroll position in
                    // the x direction first, then y on a second call. This
                    // differs from other platforms where HOME/END key only
                    // reset y scroll position, and x scroll position is
                    // preserved.
                    async function testEnd() {
                        window.scrollTo(expectedScrollX, expectedScrollY);
                        eventSender.keyDown("End");
                        await waitForCompositorCommit();
                        await waitForAnimationEndTimeBased(() => { return window.scrollX; });
                        eventSender.keyDown("End");
                        await waitForCompositorCommit();
                        await waitForAnimationEndTimeBased(() => { return window.scrollY; });
                        assertEqual("End key scrolls to bottom", window.scrollY, 0);
                    }
                    async function testHome() {
                        window.scrollTo(expectedScrollX, expectedScrollY);
                        eventSender.keyDown("Home");
                        await waitForCompositorCommit();
                        await waitForAnimationEndTimeBased(() => { return window.scrollX; });
                        eventSender.keyDown("Home");
                        await waitForCompositorCommit();
                        await waitForAnimationEndTimeBased(() => { return window.scrollY; });
                        assertEqual("Home key scrolls to top", window.scrollY, -3425);
                    }
                    await testHome();
                    await waitForCompositorCommit();
                    await testEnd();
                }

                function testSelectAll() {
                    // Test select all selects all document content.
                    window.resizeTo(500, 500);
                    var div = document.getElementById("div");
                    div.focus();
                    document.execCommand("SelectAll");
                    var selection = window.getSelection();
                    assertEqual("selectAll selects all document", selection.toString().substring(0, div.innerHTML.length), div.innerHTML);

                    finishJSTest();
                }

                testSelectAll();
            }
        </script>
    </head>
    <body>
        <div id="div" style="whitespace:nowrap; width: 1000px; height: 4000px; border: 1px solid red;">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</div>
        <ul id="console" dir=ltr></ul>
    </body>
</html>
